from my_function import *
import random
import matplotlib.pyplot as plt
import time
import sys

if __name__ == '__main__':

    np.random.seed()
    random.seed()
    #f = open("log" + str(i1), "w")
    #sys.stdout = f
    pr = set_parameters("数据集 9 6g.xlsx", 60)
    best_records = []
    start = time.time()
    get_initial_ppl(pr)
    update_best(pr)
    best_records.append(pr.best_revenue)
    for ite in range(200):
        print("")
        print("迭代次数：", ite)
        child_ppl = generate_children(pr)
        pr.ppl = select_ppl(pr.ppl, child_ppl, pr.ppl_num)
        update_best(pr)
        best_records.append(pr.best_revenue)
        if random.random() < pr.muta_rate:
            i, i1, i2 = random.sample([i for i in range(pr.ppl_num)], 3)
            chrom = pr.ppl[i]
            diff1 = pr.ppl[i1]
            diff2 = pr.ppl[i2]
            mutation(chrom, pr.best, diff1, diff2, pr.factor, len(pr.t2_list), pr)
    end = time.time()
    print("time spends: ", (end - start))
    print(pr.best_revenue)
    assert isinstance(pr.best, Chromosome)
    pr.best.fit(pr)
    """print(pr.best.sequence)
    print(pr.best.opened_t2)
    print(pr.best.term_1)
    print(pr.best.term_2)"""
    log_solution(pr)


    #f.close()

    plt.figure()
    plt.plot([i for i in range(len(best_records))], best_records)
    plt.show()
